﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;

namespace Zocono.WCS.Domain.ElevatorManage
{
    public class ElevatorMasterInfoService : IElevatorMasterInfoService
    {
        private readonly IElevatorMasterInfoRepository _IElevatorMasterInfoRepository;
        private readonly ISnowflake _Snowflake;
        public AutoMapper.IMapper _Mapper;
        private readonly ILogUtil _LogUtil;

        public ElevatorMasterInfoService(IElevatorMasterInfoRepository elevatorMasterInfoRepository
            , ISnowflake snowflake, AutoMapper.IMapper mapper, ILogUtil logUtil)
        {
            _IElevatorMasterInfoRepository= elevatorMasterInfoRepository;
            _Snowflake = snowflake;
            _Mapper = mapper;
            _LogUtil = logUtil;
        }

        public async Task<ElevatorMasterInfoDto> GetElevatorMasterInfoByIDAsync(long id)
        {
            var elevators = await _IElevatorMasterInfoRepository.GetByIdAsync(id);
            var elevatorDtos = _Mapper.Map<ElevatorMasterInfo, ElevatorMasterInfoDto>(elevators);
            return elevatorDtos;
        }

        public  async Task<PageDto<ElevatorMasterInfoDto>> GetElevatorMasterInfosAsync(ElevatorMasterInfoDto elevatorMasterInfoDto)
        {
            PageModel page = new PageModel { PageIndex = elevatorMasterInfoDto.PageIndex, PageSize = elevatorMasterInfoDto.PageSize, };
            var exp = Expressionable.Create<ElevatorMasterInfo>();
            exp.AndIF(!string.IsNullOrEmpty(elevatorMasterInfoDto.CrossroadEquipmentCode), l=>l.CrossroadEquipmentCode == elevatorMasterInfoDto.CrossroadEquipmentCode);

            return await _IElevatorMasterInfoRepository.GetElevatorMasterInfosAsync(exp.ToExpression(), page);
        }

        public async Task<bool> UpdateElevatorMasterAsync(ElevatorMasterInfoDto elevatorMasterInfoDto)
        {
            var elevatorDtos = _Mapper.Map<ElevatorMasterInfoDto, ElevatorMasterInfo>(elevatorMasterInfoDto);
            var res = await _IElevatorMasterInfoRepository.UpdateAsync(elevatorDtos);
            return res;
        }

        /// <summary>
        /// 插入提升机十字口配置
        /// </summary>
        /// <param name="elevatorInfoDto"></param>
        /// <returns></returns>
        public async Task<bool> InsertElevatorMasterAsync(ElevatorMasterInfoDto elevatorInfoDto)
        {
            var elevatorDtos = _Mapper.Map<ElevatorMasterInfoDto, ElevatorMasterInfo>(elevatorInfoDto);
            elevatorDtos.ID = _Snowflake.GetId();
            var res = await _IElevatorMasterInfoRepository.InsertAsync(elevatorDtos);
            return res;
        }

        /// <summary>
        /// 获取所有的提升机十字口配置
        /// </summary>
        /// <returns></returns>
        public async Task<List<ElevatorMasterInfoDto>> GetAllElevatorMasterInfosAsync()
        {
            try
            {
                var elevator = await _IElevatorMasterInfoRepository.GetListAsync();
                var elevatorDto = _Mapper.Map<List<ElevatorMasterInfo>, List<ElevatorMasterInfoDto>>(elevator);
                return elevatorDto;
            }
            catch (Exception ex)
            {
                _LogUtil.Error($"获取所有提升机十字口配置报错，ex={ex.Message}");
                return null;
            }
            
        }
    }
}
